import { defineStore } from 'pinia';
import { ref } from 'vue';

export interface Menu {
	[key: string]: any;
}

export const useMenuStore = defineStore(
	'menus',
	() => {

		const menuTabCache = ref<Menu[]>([]);

		const addMenuTabCache = (menu: Menu) => {
			// currentMenuID.value = menu.id;
			if (!exitsMenuTabCache(menu)) return;
			menuTabCache.value.push(menu);
		};

		const removeMenuCache = (menu: Menu) => {
			menuTabCache.value = menuTabCache.value.filter(item => item.id !== menu.id);
		};

		const exitsMenuTabCache = (menu: Menu) => {
			return menuTabCache.value.findIndex((item) => item.id === menu.id) === -1;
		};

		const $reset = () => {
			menuTabCache.value = [];
		};

		return {
			menuTabCache,
			addMenuTabCache,
			removeMenuCache,
			$reset
		};
	},
	{
		// persist: true,
		persist: {
			storage: sessionStorage
		}
	}
);
